package cn.com.bluemoon.daps.model.controller.api;

import cn.com.bluemoon.daps.api.model.RemoteHandlerStdRelateService;
import cn.com.bluemoon.daps.common.constant.DapConstant.DapStandard.StandardType;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants.DapsApiModel;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.exception.DapThrowException;
import cn.com.bluemoon.daps.model.entity.DapDataModelTableField;
import cn.com.bluemoon.daps.model.service.DapDataModelTableFieldService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Jarod Kong
 */
@Api(tags = {"FeignClient服务-模型去除标准信息-api接口"})
@RequestMapping(DapsApiModel.API_MODEL_STANDARD)
@BmPermission(value = "", name = "服务注册-模型关联标准", ignore = true)
@RestController
@Slf4j
public class DapDataModelHandlerStdServiceApi implements RemoteHandlerStdRelateService {
    @Resource
    private DapDataModelTableFieldService dapDataModelTableFieldService;

    @PutMapping("/clearRelate")
    public ResultBean<Boolean> clearRelation(@RequestParam("standardUniqueId") String standardUniqueId,
                                             @RequestParam("standardType") StandardType standardType) {
        if (StandardType.BASIC.equals(standardType)) {
            dapDataModelTableFieldService.update(new UpdateWrapper<DapDataModelTableField>().set("standard_id", '0').set("standard_method", null).eq("standard_id", standardUniqueId));
            // update table_name set standard_id where standard_id=?
            // delete from table_name where standard_id=?
            // ... etc.
            return ResultBean.ok(true);
        } else {
            throw new DapThrowException("模型中不支持清理非基础标准的关系");
        }
    }
}
